//
//  SAWAllCorrectHomework.m
//  BestLearning
//
//  Created by edz on 2018/2/1.
//  Copyright © 2018年 apple. All rights reserved.
//
#import "CorrectWebVC.h"
#import "SAWCorrectModel.h"
#import "CorrectTabCell.h"
#import "SAWAllCorrectHomework.h"
#import "CorrectHomeWorkModel.h"
@interface SAWAllCorrectHomework ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)NSMutableArray *dataItemArr;
@property(nonatomic,strong)SAWCorrectModel *model;
@property (nonatomic,assign) NSInteger pageNum;
@property (nonatomic,assign)BOOL isUpRefresh;
@property(nonatomic,strong)UITableView *tableView;
@end

@implementation SAWAllCorrectHomework
-(NSMutableArray *)dataItemArr
{
    if (!_dataItemArr) {
        _dataItemArr = [[NSMutableArray alloc]init];
    }
    
    return _dataItemArr;
}
-(void)popHomeView
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"错题集中营";
    
 self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithTargat:self action:@selector(popHomeView) image:@"icon_back_default1" highImage:nil];
    [self setUI];
}
-(SAWCorrectModel *)model
{
    if (!_model) {
        _model = [[SAWCorrectModel alloc]init];
    }
    return _model;
}
-(void)setUI
{
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight-kTopHeight)];
    [self.view addSubview:_tableView];
    _tableView.backgroundColor = RGBColor(245, 245, 245);
    [_tableView registerClass:[CorrectTabCell class] forCellReuseIdentifier:@"Cell"];
    _tableView.delegate = self;
     _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.dataSource = self;
    _tableView.rowHeight=110;
    //上拉加载
   // _tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(downRefresh)];
    //下拉刷新
   // _tableView.mj_header  = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(upRefresh)];
    _pageNum = 1;
    [self loadData];
   
}
-(void)loadData
{
    [self.model CorrectHomeWork:^(NSArray *dataArr) {
        if (_isUpRefresh) {
            [self.dataItemArr removeAllObjects];
        }
        
        for (NSDictionary *dic in dataArr) {
            CorrectHomeWorkModel *model = [CorrectHomeWorkModel mj_objectWithKeyValues:dic];
            model.ID = [dic objectForKey:@"id"];
            [self.dataItemArr addObject:model];
        }
        [_tableView reloadData];
        [_tableView.mj_footer endRefreshing];
        [_tableView.mj_header endRefreshing];
    } withpage:_pageNum withSearchName:self.ID withFild:^{
        [_tableView.mj_footer endRefreshing];
        [_tableView.mj_header endRefreshing];
    }];
    

}
-(void)upRefresh
{
    _pageNum ++;
    _isUpRefresh=NO;
    [self loadData];
    
}
-(void)downRefresh
{
    _pageNum = 1;
    _isUpRefresh=YES;
    [self loadData];
    
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataItemArr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CorrectTabCell *cell =[tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    CorrectHomeWorkModel *model = self.dataItemArr[indexPath.row];
    cell.titLab.text=model.knowledgeName;
    cell.correctLab.text = [NSString stringWithFormat:@"已有错题:%@",model.notrightcount];
    NSLog(@"====%@",model.precent);
   
   
    [cell.strongBtn setTitle:@"开设加强课" forState:0];
    [cell.strongBtn addTarget:self action:@selector(alertStrongView:) forControlEvents:UIControlEventTouchUpInside];
   
    return cell;
}
-(void)alertStrongView:(UIButton *)sender
{
    
    
    
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    CorrectHomeWorkModel *model = self.dataItemArr[indexPath.row];
    CorrectWebVC *vc = [CorrectWebVC new];
    vc.title=model.knowledgeName;
    vc.urlString = [NSString stringWithFormat:@"user.do?action=getQuestiontByKnowledgeId&knowledgeId=%@&type=4",model.ID];
    [self.navigationController pushViewController:vc animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
